package com.cytech.cloudclinic.moudle.main.clinic.admission.report;

import android.Manifest;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

import androidx.annotation.Nullable;

import com.cytech.cloudclinic.R;
import com.cytech.cloudclinic.base.BaseFragment;
import com.cytech.cloudclinic.base.BasePresenter;
import com.cytech.cloudclinic.bean.LoadImageEntity;
import com.cytech.cloudclinic.bean.PatientInformation;
import com.cytech.cloudclinic.bean.UploadImage;
import com.cytech.cloudclinic.bean.req.AddPrescription;
import com.cytech.cloudclinic.moudle.main.clinic.admission.AdmissionStyleFragment;
import com.cytech.cloudclinic.moudle.main.clinic.admission.intelligent.IntelligentFragment;
import com.cytech.cloudclinic.utils.GetPathFromUri;
import com.cytech.cloudclinic.utils.MatisseUtils;
import com.cytech.cloudclinic.view.SelectImageView;
import com.nabinbhandari.android.permissions.PermissionHandler;
import com.nabinbhandari.android.permissions.Permissions;
import com.zhihu.matisse.Matisse;
import com.zxy.tiny.Tiny;
import com.zxy.tiny.callback.FileCallback;

import java.io.File;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

import static android.app.Activity.RESULT_OK;

/**
 * 查体记录及图片
 */
public class ReportRecordFragment extends BaseFragment<ReportRecordPresenter> implements ReportRecordView,SelectImageView.OnItemImageSelectListener{

    @BindView(R.id.et_body_report_1)
    EditText et1;
    @BindView(R.id.et_body_report_2)
    EditText et2;
    @BindView(R.id.et_body_report_3)
    EditText et3;
    @BindView(R.id.et_body_report_4)
    EditText et4;
    @BindView(R.id.et_body_report_5)
    EditText et5;
    @BindView(R.id.et_body_report_6)
    EditText et6;
    @BindView(R.id.et_body_report_7)
    EditText et7;
    @BindView(R.id.et_body_report_8)
    EditText et8;
    @BindView(R.id.et_body_report_9)
    EditText et9;

    @BindView(R.id.st_select_image_view)
    SelectImageView selectImageView;
    @BindView(R.id.st_select_image_report)
    SelectImageView reportSeleteImageView;
    List<Uri> mSelected;

    private AddPrescription prescription;

    //选取图片
    private int REQUEST_CODE_CHOOSE = 1001;

    private int TAG_TONGUE = 1009;
    private int TAG_REPORT = 1008;
    //标记是哪一个控件调用的打开相册
    private int currentTag;


    public static ReportRecordFragment newInstance(AddPrescription prescription) {
        ReportRecordFragment fragment = new ReportRecordFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable("prescription", prescription);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected ReportRecordPresenter createPresenter() {
        return new ReportRecordPresenter(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_admission_report_record;
    }

    @Override
    protected void initView() {
        prescription = (AddPrescription) getArguments().getSerializable("prescription");
        selectImageView.setOnItemSelectListener(this,TAG_TONGUE);
        reportSeleteImageView.setOnItemSelectListener(this,TAG_REPORT);
    }

    @Override
    protected void initData() {

    }



    //@SuppressWarnings("deprecation")
    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        //super.onActivityResult(requestCode, resultCode, data);
        Log.d("TAG", requestCode+"onActivityResult: "+resultCode);
        if (resultCode == RESULT_OK) {
            if (requestCode == REQUEST_CODE_CHOOSE) {
                mSelected = Matisse.obtainResult(data);
                if (mSelected != null && mSelected.size() > 0) {
                    uploadImage((mSelected.get(0)));
                }
            }
        }
    }

    public void uploadImage(Uri uri) {
        Tiny.FileCompressOptions options = new Tiny.FileCompressOptions();
        options.size = 100;
        Tiny.getInstance().source(GetPathFromUri.getPath(getActivity(), uri)).asFile().withOptions(options).compress(new FileCallback() {
            @Override
            public void callback(boolean isSuccess, String outfile, Throwable t) {
                Log.d("TAG", "callback: " + new File(outfile).length());
                presenter.uploadImage(getActivity(), outfile, uri);
            }
        });
    }

    @Override
    public void loadSuccess(LoadImageEntity url, Uri uri) {
        Log.d("TAG", "loadSuccess: "+"图片上传成功");
        hideLoading();
        //图片上传成功
        UploadImage uploadImage = new UploadImage();
        uploadImage.localUri = uri;
        uploadImage.url = url.url;
        if(currentTag == TAG_TONGUE){
            selectImageView.notifySelectData(uploadImage);
        }else {
            reportSeleteImageView.notifySelectData(uploadImage);
        }
        mSelected.remove(uri);
        if (mSelected.size() > 0) {
            uploadImage(mSelected.get(0));
        }
    }

    @OnClick(R.id.btn_report_next)
    public void nextClick(View v) {
        PatientInformation information = new PatientInformation();
        information.BP_L = et1.getText().toString();
        information.BP_H = et2.getText().toString();
        information.P = et3.getText().toString();
        information.BG = et4.getText().toString();
        information.R = et5.getText().toString();
        information.SH = et6.getText().toString();
        information.T = et7.getText().toString();
        information.W = et8.getText().toString();
        information.other = et9.getText().toString();
        prescription.patient_information = information;

        ((AdmissionStyleFragment)getParentFragment()).switchPrescriptionFragment(prescription);
    }


    @Override
    public void onImageSelectSelect(int seleteTag) {
        currentTag = seleteTag;
        String[] permissions = {Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA};
        Permissions.check(getActivity(), permissions, null, null, new PermissionHandler() {
            @Override
            public void onGranted() {
                MatisseUtils.getInstance().openPicker(ReportRecordFragment.this, 1, true, REQUEST_CODE_CHOOSE);
            }
        });
    }

    @Override
    public void onImageDelete(int position,int seleteTag) {
        currentTag = seleteTag;
    }
}
